﻿using System;
using System.Linq.Expressions;

namespace NanoMvvm
{
    public static class ExpressionResolver
    {
        public static string GetMemberName<TMmeber>(Expression<Func<TMmeber>> memberExpression)
        {
            if (memberExpression == null)
            {
                throw new ArgumentNullException("memberExpression");
            }

            var body = (MemberExpression)((memberExpression.Body is UnaryExpression)
               ? ((UnaryExpression)memberExpression.Body).Operand : memberExpression.Body);

            return body.Member.Name;
        }

        public static string GetMemberName<T, TMmeber>(Expression<Func<T, TMmeber>> memberExpression)
        {
            if (memberExpression == null)
            {
                throw new ArgumentNullException("memberExpression");
            }

            var body = (MemberExpression)((memberExpression.Body is UnaryExpression)
               ? ((UnaryExpression)memberExpression.Body).Operand : memberExpression.Body);

            if (body == null)
            {
                throw new ArgumentException("The body must be a member expression");
            }

            return body.Member.Name;
        }
    }
}